//导入Node.js官方提供的http模块
import { createServer } from 'node:http'

//创建Web服务器，接收客户端请求(request)，返回响应消息(response)
const server = createServer((req, res) => {
  //读取客户端请求消息中的“请求地址”，根据请求地址调用不同的处理方法
  if(req.url=='/login'){
	  doLogin(req, res)
  }else if(req.url=='/user/register'){
	  doRegister(req, res)
  }else if(req.url=='/comment/list'){
	  doCommentList(req, res)
  }else {
	  res.writeHead(200, { 'Content-Type': 'application/json' })
	  res.end('{code:404, msg:"url not found"}')
  }
})

function doLogin(req, res){		//处理“登录”请求
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end('{code:4000, msg:"login success"}')
}
function doRegister(req, res){	//处理“注册”请求
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end('{code:5000, msg:"register success"}')
}
function doCommentList(req, res){	//处理“获取评论列表”请求
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end('[{cid:10,content:"Good"},{cid:15,content:"VeryGood"}]')
}

//启动Web服务器，让它监听8081
server.listen(8081, () => {
  console.log('Listening on Port 8081...')
})
